from django.db import models
from django.utils.translation import gettext_lazy as _

# Create your models here.

class WechatUserInfo(models.Model):
    openid = models.CharField(_('微信OpenID'), max_length=128, unique=True)
    nickname = models.CharField(_('昵称'), max_length=128, blank=True)
    avatar_url = models.URLField(_('头像'), blank=True)
    gender = models.SmallIntegerField(_('性别'), choices=[(0, '未知'), (1, '男'), (2, '女')], default=0)
    city = models.CharField(_('城市'), max_length=64, blank=True)
    province = models.CharField(_('省份'), max_length=64, blank=True)
    country = models.CharField(_('国家'), max_length=64, blank=True)
    created_at = models.DateTimeField(_('创建时间'), null=True, blank=True)

    class Meta:
        verbose_name = _('微信用户信息')
        verbose_name_plural = _('微信用户信息')

class WechatPayment(models.Model):
    STATUS_CHOICES = [
        ('pending', '待支付'),
        ('success', '支付成功'),
        ('failed', '支付失败'),
        ('refunded', '已退款')
    ]
    
    user = models.ForeignKey(WechatUserInfo, on_delete=models.CASCADE, verbose_name=_('用户'))
    transaction_id = models.CharField(_('微信支付订单号'), max_length=32, unique=True)
    out_trade_no = models.CharField(_('商户订单号'), max_length=32)
    total_fee = models.DecimalField(_('支付金额'), max_digits=10, decimal_places=2)
    status = models.CharField(_('支付状态'), max_length=16, choices=STATUS_CHOICES, default='pending')
    created_at = models.DateTimeField(_('创建时间'), null=True, blank=True)
    updated_at = models.DateTimeField(_('更新时间'), null=True, blank=True)
    ip_address = models.GenericIPAddressField(_('IP地址'))
    location = models.CharField(_('位置'), max_length=128, blank=True)

    class Meta:
        verbose_name = _('微信支付记录')
        verbose_name_plural = _('微信支付记录')

# 微信小程序资源库
class AppResource(models.Model):
    RESOURCE_TYPES = [
        ('model', '模型'),
        ('api', 'API地址')
    ]

    PAGE_CHOICES = [
        ('chat_emo', '情感对话'),
        ('data_bank', '数据银行'),
        ('emo_test', '情感测试')
    ]

    name = models.CharField(_('资源名称'), max_length=64)
    page = models.CharField(_('所属页面'), max_length=32, choices=PAGE_CHOICES, default='chat_emo')
    type = models.CharField(_('资源类型'), max_length=16, choices=RESOURCE_TYPES)
    key = models.CharField(_('资源键名'), max_length=64, unique=True)
    value = models.TextField(_('资源值'))
    description = models.TextField(_('描述'), blank=True)
    is_active = models.BooleanField(_('是否启用'), default=True)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)

    class Meta:
        verbose_name = _('应用资源')
        verbose_name_plural = _('应用资源')
        ordering = ['page', 'type', 'name']

    def __str__(self):
        return f"{self.get_page_display()}-{self.get_type_display()}-{self.name}"
